<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>选择添加收货方式-{eyou:global name='web_name' /}</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<link href="{eyou:global name='web_cmspath' /}/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    {eyou:static file="users/skin/css/basic.css" /}
    {eyou:static file="users/skin/css/eyoucms.css" /}
    {eyou:include file="users/skin/css/diy_css.htm" /}
    
    {eyou:static file="/public/static/common/js/jquery.min.js"/}
    {eyou:static file="/public/plugins/layer-v3.1.0/layer.js"/}
    {eyou:static file="users/skin/js/global.js" /}
</head>

<body class="reg">
<div class="register_index ey-member">
    <div class="container">
        {eyou:empty name="$is_wechat_applets"}
            <!-- 为空则表示在微信端中但不在微信小程序中 -->
            <button class="btn btn-lg btn-primary btn-block" data-url="{$eyou.field.wechat_url}" style="width: 100%;" onclick="GetWechatData(this);" >
                获取微信地址
            </button>
        {/eyou:empty}
        <br/>
        <button class="btn btn-lg btn-primary btn-block" data-url="{$eyou.field.add_addr_url}" style="width: 100%;" onclick="ShopAddAddress(this);">
            添加本站地址
        </button>
    </div>
</div>

<script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript">
   // 获取微信调用接口数据
   function GetWechatData(ey_this){
        var wechat_url = $(ey_this).attr('data-url');
        if (!wechat_url) {
            showErrorAlert('网络失败，请刷新页面后重试');
        }
        layer_loading('正在处理');
        $.ajax({
            url: wechat_url,
            data: {data:0},
            type: 'post',
            dataType:'json',
            success:function(res){
                layer.closeAll();
                if (res.code == 1) {
                    GetAddr(res.data);
                }else{
                    showErrorAlert(res.msg);
                }
            },
            error : function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
    
    function GetAddr(data){
        wx.config({
            appId:     data.appid,
            timestamp: data.timestamp,
            nonceStr:  data.noncestr,
            signature: data.signature,
            jsApiList: [
                'checkJsApi',
                'openAddress',
                // 'chooseAddress',
            ]
        }); 

        wx.ready(function(){
            wx.openAddress({
                success: function (res) {
                    // 地址同步到本地数据库中
                    AddWechatAddress(res);
                },
                cancel: function () {
                    layer.msg('不使用微信地址，请添加本站地址！', {time: 2000});
                }
            });
        });
    }

    function AddWechatAddress(addr){
        if (!addr) {
            layer.msg('获取数据错误，请刷新重试~~', {time: 2000});
        }
        $.ajax({
            url: "{eyou:url link='user/Shop/add_wechat_addr'/}",
            data: {
                userName    :addr.userName,
                telNumber   :addr.telNumber,
                provinceName:addr.provinceName,
                cityName    :addr.cityName,
                countryName :addr.countryName,
                detailInfo  :addr.detailInfo,
            },
            type:'post',
            dataType:'json',
            success:function(res){
                if (1 == res.code) {
                    layer.msg(res.msg, {time: 500}, function(){
                        window.location.href = res.url;
                    });
                }else{
                    showErrorAlert(res.msg);
                }
            }
        });
    }

    // 添加收货地址
    function ShopAddAddress(ey_this){
        var add_addr_url = $(ey_this).attr('data-url');
        if (!add_addr_url) {
            showErrorAlert('网络失败，请刷新页面后重试');
        }

        var url = add_addr_url;
        if (url.indexOf('?') > -1) {
            url += '&';
        } else {
            url += '?';
        }
        url += 'type=order_new';
        //iframe窗
        layer.open({
            type: 2,
            title: '添加收货地址',
            shadeClose: false,
            maxmin: false, //开启最大化最小化按钮
            area: ['100%', '100%'],
            content: url
        });
    }

    function ReturnUrl(url){
        window.location.href = url;
    }
</script>
</body>
</html>